package com.cool.reggie.modules.dish.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cool.reggie.modules.category.entity.CategoryEntity;
import com.cool.reggie.modules.dish.dto.DishDto;
import com.cool.reggie.modules.dish.entity.DishEntity;
import com.cool.reggie.modules.dish.mapper.DishMapper;
import com.cool.reggie.modules.dish.service.DishService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cool.reggie.modules.dishFlavor.entity.DishFlavorEntity;
import com.cool.reggie.modules.dishFlavor.service.DishFlavorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜品管理 服务实现类
 * </p>
 *
 * @author 1950735817@qq.com
 * @since 2022-07-06
 */
@Slf4j
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, DishEntity> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;


    /**
     * 保存与味道
     *
     * @param dishDto 菜dto
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveWithFlavor(DishDto dishDto) {


        log.info(dishDto.toString());
        DishEntity dishEntity = new DishEntity();
        BeanUtils.copyProperties(dishDto, dishEntity);

        // 保存菜品信息
        this.save(dishDto);

        Long dishDtoId = dishDto.getId();

        List<DishFlavorEntity> flavors = dishDto.getFlavors();
        // 设置菜品ID
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishDtoId);
            return item;
        }).collect(Collectors.toList());

        // 保存口味
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public DishDto getByIdWithFlavor(Long id) {
        DishEntity dish = this.getById(id);

        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);

        LambdaQueryWrapper<DishFlavorEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavorEntity::getDishId, dish.getId());

        List<DishFlavorEntity> list = dishFlavorService.list(queryWrapper);

        dishDto.setFlavors(list);

        return dishDto;
    }

    @Override
    public void updateWithFlavor(DishDto dishDto) {
        this.updateById(dishDto);

        LambdaQueryWrapper<DishFlavorEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavorEntity::getDishId, dishDto.getId());

        dishFlavorService.remove(queryWrapper);

        List<DishFlavorEntity> flavors = dishDto.getFlavors();

        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(flavors);

    }
}
